var prefix = jsctx + "/tenantTicketType";

var sendRecord = (function () {
    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        load();
    };

    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    function load() {
        $('#exampleTable')
            .bootstrapTable({
                method: 'get',
                url: prefix + "/listPageSendRecord",
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: false,
                dataType: "json",
                pagination: true,
                singleSelect: false,
                pageSize: 20,
                pageList: [20],
                pageNumber: 1,
                sidePagination: "server",
                cache: false,
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: (params.offset / params.limit) + 1,
                        ticketTypeId: $("#ticketTypeId").val().trim()
                    };
                },
                columns: [
                    {
                        field: 'number',
                        title: '序号',
                        width: '100', align: 'center',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },

                    {field: 'ticketName', title: '优惠券名称', align: 'left', width: '100'},
                    {
                        field: 'ticketType',
                        title: '优惠券类型',
                        width: '100', align: 'left',
                        formatter: function (value, row, index) {
                            if (row.ticketType == '0') {
                                return '充电优惠券';
                            } else if (row.ticketType == '1') {
                                return '充值优惠券';
                            }
                        }
                    },
                    {
                        field: 'useRequireAmount',
                        title: '使用条件',
                        width: '100', align: 'left',
                        formatter: function (value, row, index) {
                            return '满' + row.useRequireAmount;
                        }
                    },
                    {
                        field: 'reducedAmount',
                        title: '优惠',
                        width: '100', align: 'left',
                        formatter: function (value, row, index) {
                            if (row.discountType == '0') {
                                return '立减' + row.discountAmount;
                            } else if (row.discountType == '1') {
                                return row.discount + '折';
                            }
                        }
                    },
                    {
                        field: 'onceOrRepeatedly',
                        title: '发送方式',
                        width: '100', align: 'left',
                        formatter: function (value, row, index) {
                            if (row.onceOrRepeatedly == '0') {
                                return '单次';
                            } else if (row.onceOrRepeatedly == null) {
                                return '单次';
                            } else if (row.onceOrRepeatedly == '1') {
                                return '多次';
                            }
                        }
                    },
                    {
                        field: '',
                        title: '定时发送',
                        width: '100', align: 'left',
                        formatter: function (value, row, index) {
                            if (row.sendType == '0') {
                                return '即时' + row.sendTime;
                            } else if (row.sendType == '1') {
                                return '定时' + row.firstSendTime;
                            }
                        }
                    },
                    {field: 'sendCount', title: '赠送数量', align: 'left',width: '100'},
                    {
                        field: '', title: '赠送频率', align: 'left',
                        width: '100', align: 'left',
                        formatter: function (value, row, index) {
                            if (row.schedulePeriod == '0') {
                                return row.periodCount + '张/日';
                            } else if (row.schedulePeriod == '1') {
                                return row.periodCount + '张/周';
                            } else if (row.schedulePeriod == '2') {
                                return row.periodCount + '张/月';
                                ;
                            }
                        }
                    },
                    {field: 'ticketScheduleCount', title: '发放次数', align: 'left',width: '100'},
                    {
                        field: '', title: '状态', align: 'left',
                        width: '100', align: 'left',
                        formatter: function (value, row, index) {
                            if (row.sendType == '1') {
                                if (row.remainCount == 0) {
                                    return '发送完成';
                                } else if (row.remainCount == row.ticketScheduleCount) {
                                    return '未发送';
                                } else if (row.remainCount != row.ticketScheduleCount) {
                                    return '发送中';
                                }
                            } else {
                                return '-';
                            }
                        }
                    },
                    {
                        title: '操作',
                        field: 'orderId',
                        width: '200', align: 'center',
                        formatter: function (value, row, index) {

                            var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="定时赠送" onclick="sendRecord.detialSendRecord(\''
                                + row.ticketSendRecordId
                                + '\')">详情</a> ';

                            return a;

                        }
                    }
                ]
            });
    };

    function detialSendRecord(ticketSendRecordId) {
        location.href = prefix + "/detialSendRecord/" + ticketSendRecordId;
    }

    return {
        init: init,
        reLoad: reLoad,
        detialSendRecord: detialSendRecord
    };
})();

